package team.tunan.controller;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import team.tunan.common.DataJson;
import team.tunan.utils.UploadUtils;

import java.io.IOException;
import java.util.HashMap;

/**
 * @Date 2022/7/9
 * @Author ztc
 * @Description TODO
 */

@Controller
@RequestMapping("/upload")
public class UploadController {

    @RequestMapping("/image")
    @ResponseBody
    public DataJson uploadImage(@RequestParam(value = "file") MultipartFile file) throws IOException {
        //调用我们写的上传文件的工具类
        String imagePath = UploadUtils.upload(file);//获得图片路径
        DataJson dataJson = new DataJson();
        if(imagePath!=null) {
//            model.addAttribute("imgPath","imagePath");
            dataJson.setCode(1);
            dataJson.setMsg("上传成功");
            HashMap<String,String> map = new HashMap<>();
            map.put("src",imagePath);
            dataJson.setData(map);
        }else {
            dataJson.setCode(0);
            dataJson.setMsg("对不起，上传失败");
        }
        return dataJson;
    }


}
